//
//  MKPushKit.m
//  YangDongXi
//
//  Created by cocoa on 15/5/13.
//  Copyright (c) 2015年 yangdongxi. All rights reserved.
//

#import "MKPushKit.h"
#import "APService.h"
#import "AppDelegate.h"
#import "MKUrlGuide.h"

@implementation MKPushKit

+ (void)setLogOFF
{
    [APService setLogOFF];
}

+ (void)applicationDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0)
    {
        //可以添加自定义categories 注册这个APNS
        [APService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
                                                       UIUserNotificationTypeSound |
                                                       UIUserNotificationTypeAlert)
                                           categories:nil];
    }
    else
    {
        //categories 必须为nil
        [APService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                       UIRemoteNotificationTypeSound |
                                                       UIRemoteNotificationTypeAlert)
                                           categories:nil];
    }
    // 初始化
    [APService setupWithOption:launchOptions];
}

+ (void)applicationDidRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    // 向服务器报devide_Token
    [APService registerDeviceToken:deviceToken];
    NSInteger userId = getUserCenter.userInfo.userId;
    NSUserDefaults *userDefaults =  [NSUserDefaults standardUserDefaults];
    if ([userDefaults objectForKey:@"alias"]) {
        [APService setAlias:[userDefaults objectForKey:@"alias"] callbackSelector:nil object:nil];
    }
//    if (userId > 0)
//    {
//        [APService setAlias:@"13673356641" callbackSelector:nil object:nil];
//    }
}

+ (void)applicationDidReceiveRemoteNotification:(NSDictionary *)userInfo
{
    // 处理收到的APNS消息，向服务器上报收到APNS消息
    [APService handleRemoteNotification:userInfo];
}

+ (void)applicationDidReceiveRemoteNotification:(NSDictionary *)userInfo
                         fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    [APService handleRemoteNotification:userInfo];
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    if ([[userDefaults objectForKey:@"isAppInBackground"] intValue] == 1) {
        
    }
    completionHandler(UIBackgroundFetchResultNewData);
    if ([[MKUrlGuide commonGuide] canHandle:userInfo[@"URL"]])
    {
        [[MKUrlGuide commonGuide] guideForUrl:userInfo[@"URL"]];
    }
    
    
}

@end
